package com.book.bookprojectapplication.login.activity;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.book.bookprojectapplication.R;


public class ScreenViewFlipper extends LinearLayout implements OnTouchListener {

	/**
	 * Count of index buttons. Default is 3
	 */
	public static int countIndexes = 4;

	/**
	 * Button Layout
	 */
	LinearLayout buttonLayout;

	/**
	 * Index button images
	 */
	ImageView[] indexButtons;

	/**
	 * Views for the Flipper
	 */
	View[] views;

	/**
	 * Flipper instance
	 */
	ViewFlipper flipper;

	/**
	 * X coordinate for touch down
	 */
	float downX;

	/**
	 * X coordinate for touch up
	 */
	float upX;

	/**
	 * Current index
	 */
	int currentIndex = 0;

	public ScreenViewFlipper(Context context) {
		super(context);

		init(context);
	}

	public ScreenViewFlipper(Context context, AttributeSet attrs) {
		super(context, attrs);

		init(context);
	}

	/**
	 * Initialize
	 * 
	 * @param context
	 */
	public void init(Context context) {

		// Layout inflation
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.screenview, this, true);

		buttonLayout = (LinearLayout) findViewById(R.id.buttonLayout);
		flipper = (ViewFlipper) findViewById(R.id.flipper);
		flipper.setOnTouchListener(this);

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		// params.leftMargin = 50;

		indexButtons = new ImageView[countIndexes];
		views = new TextView[countIndexes];

		for (int i = 0; i < countIndexes; i++) {
			indexButtons[i] = new ImageView(context);

			if (i == currentIndex) {
				indexButtons[i].setImageResource(R.drawable.green);
			} else {
				indexButtons[i].setImageResource(R.drawable.white);
			}
			buttonLayout.addView(indexButtons[i], params);

		}

	}

	/**
	 * Update the display of index buttons
	 */
	private void updateIndexes() {
		for (int i = 0; i < countIndexes; i++) {
			if (i == currentIndex) {
				indexButtons[i].setImageResource(R.drawable.green);
			} else {
				indexButtons[i].setImageResource(R.drawable.white);
			}
		}
	}

	/**
	 * onTouch event handling
	 */
	public boolean onTouch(View v, MotionEvent event) {
		if (v != flipper)
			return false;

		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			downX = event.getX();
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			upX = event.getX();

			if (upX < downX) { // in case of right direction

				flipper.setInAnimation(AnimationUtils.loadAnimation(
						getContext(), R.anim.push_left_in));
				flipper.setOutAnimation(AnimationUtils.loadAnimation(
						getContext(), R.anim.push_left_out));

				if (currentIndex < (countIndexes - 1)) {
					flipper.showNext();

					// update index buttons
					currentIndex++;
					updateIndexes();
				}
			} else if (upX > downX) { // in case of left direction

				flipper.setInAnimation(AnimationUtils.loadAnimation(
						getContext(), R.anim.push_right_in));
				flipper.setOutAnimation(AnimationUtils.loadAnimation(
						getContext(), R.anim.push_right_out));

				if (currentIndex > 0) {
					flipper.showPrevious();

					// update index buttons
					currentIndex--;
					updateIndexes();
				}
			}
		}

		return true;
	}

}
